/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){
    
    $('input#submit').click(function(){
        var name = "&name=" + $('input[name="product_name"]').val();
        var cat = "&cat=" + $("select[name=product_cat]").find("option:selected").val();
        var price = "&price=" + $('input[name="product_price"]').val();
        var desc = "&desc=" + $('textarea[name="product_desc"]').val();
        
        var image1 =  $("input[name=product_image1]").val();
        image1 = "&image1=" + image1.split("\\")[2];
        var image2 =$("input[name=product_image2]").val();
        image2 = "&image2=" + image2.split("\\")[2];
        var image3 =$("input[name=product_image3]").val();
        image3 = "&image3=" + image3.split("\\")[2];
        
        var data = "Action=AddProduct" + name + cat + price + desc +image1 +image2 +image3;
        $.ajax({
            type:"post",
            url:"AdminController",
            data: data,
            async: false
        });
    });
    $('input#submitEdit').click(function(){
        var id = "&idProduct=" + $('input[name="product_id"]').val();
        var name = "&name=" + $('input[name="product_name"]').val();
        var cat = "&cat=" + $("select[name=product_cat]").find("option:selected").val();
        var price = "&price=" + $('input[name="product_price"]').val();
        var desc = "&desc=" + $('textarea[name="product_desc"]').val();
        var image1 =  $("input[name=product_image1]").val();
        var image2 =  $("input[name=product_image2]").val();
        var image3 =  $("input[name=product_image3]").val();
        
        
         if($("input[name=product_image1]").val() != undefined){
             image1 = "&image1=" + image1.split("\\")[2];
         }else{
             image1 = "&image1=" + $('img#image1').attr("src");
         }
        if($("input[name=product_image2]").val() != undefined){
             image2 = "&image2=" + image2.split("\\")[2];
         }else{
             image2 = "&image2=" + $('img#image2').attr("src");
         }
         if($("input[name=product_image3]").val() != undefined){
             image3 = "&image3=" + image3.split("\\")[2];
         }else{
             image3 = "&image3=" + $('img#image3').attr("src");
         }
        var data = "Action=EditProduct" + id + name + cat + price + desc +image1 +image2 +image3;
        $.ajax({
            type:"post",
            url:"AdminController",
            data: data,
            async: false
        });
    });
    
    
    
    /*
    $('a.icon-2').click(function(){
        var idCat = $(this).attr('alt');
        $.ajax({
            url:"AdminController",
            type:"post",
            data:"Action=DeleteCat&idCat=" + idCat,
            success: function(rs){
                rs = jQuery.trim(rs);
                if(rs=="OK"){
                    $(this).parent().remove();
                }
            }
        });
        return false;
    });
    */
    $('a.delProduct').click(function(){
        var idProduct = $(this).attr('alt');
        $.ajax({
            type:"post",
            url:"AdminController",
            data: "Action=DelProduct&idProduct=" + idProduct,
            success: function(rs){
                rs = jQuery.trim(rs);
                if(rs=="ok"){
                    $(this).parent().parent().remove();
                }
            }
        });
        return false;
    });
    
});